//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//********************************************************* 

using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml;
using Microsoft.TridentWordAddIn.ResourceHelper;

namespace Microsoft.TridentWordAddIn.Presentation
{
    internal class ConnectionXmlReader
    {
        /// <summary>
        /// XmlDocument object to hold the connection specific information
        /// </summary>
        private XmlDocument xmlDocument;

        /// <summary>
        /// Initializes a new instance of the <see cref="ConnectionXmlReader"/> class.
        /// </summary>
        public ConnectionXmlReader()
        {
            this.xmlDocument = new XmlDocument();
            this.xmlDocument.LoadXml(ResourceFetcher.GetString("TridentConnection"));
        }

        /// <summary>
        /// Gets the source names.
        /// </summary>
        /// <returns>IList of type string</returns>
        public IList<string> GetDataSources()
        {
            XmlNodeList sourceList = this.xmlDocument.SelectNodes("Connection/Source");
            IList<string> dataSoureList = new List<string>();
            foreach (XmlNode source in sourceList)
            {
                dataSoureList.Add(source.Attributes["name"].Value);
            }

            return dataSoureList;
        }

        /// <summary>
        /// Gets the name of the selected source.
        /// </summary>
        /// <returns>string</returns>
        public string GetSelectedSourceName()
        {
            XmlNode selectedNode = this.xmlDocument.SelectSingleNode("Connection/Source[@selected='true']");
            if (selectedNode != null)
            {
                return selectedNode.Attributes["name"].Value;
            }

            return null;
        }

        /// <summary>
        /// Determines whether the specified source name is anonymous.
        /// </summary>
        /// <param name="dataSource">The data source.</param>
        /// <param name="type">The type.</param>
        /// <returns>
        /// 	<c>true</c> if the specified source name is anonymous; otherwise, <c>false</c>.
        /// </returns>
        public bool IsAnonymous(string dataSource, string type)
        {
            string query = "Connection/Source[@name='" + dataSource + "']/" + type;
            XmlNode xmlNode = this.xmlDocument.SelectSingleNode(query);
            if (xmlNode.Attributes["anonymous"] != null && xmlNode.Attributes["anonymous"].Value == "true")
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Determines whether the specified source name is trusted.
        /// </summary>
        /// <param name="dataSource">The data source.</param>
        /// <param name="type">The type.</param>
        /// <returns>
        /// 	<c>true</c> if the specified source name is trusted; otherwise, <c>false</c>.
        /// </returns>
        public bool IsTrusted(string dataSource, string type)
        {
            string query = "Connection/Source[@name='" + dataSource + "']/" + type;
            XmlNode xmlNode = this.xmlDocument.SelectSingleNode(query);
            if (xmlNode.Attributes["trusted"] != null && xmlNode.Attributes["trusted"].Value == "true")
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// Gets the control object.
        /// </summary>
        /// <param name="dataSource">The data source.</param>
        /// <param name="type">The type.</param>
        /// <returns>UserControl</returns>
        public UserControl GetControlObject(string dataSource, string type)
        {
            UserControl resultControl = null;
            string xquery = "Connection/Source[@name='" + dataSource + "']/" + type;
            XmlNode xmlNode = this.xmlDocument.SelectSingleNode(xquery);
            if (xmlNode != null)
            {
                switch (xmlNode.Attributes["type"].Value)
                {
                    case "AmazonWebServiceAcctInfo":
                        WebServiceAccountInformation connectionAcctInfoWebService = new WebServiceAccountInformation();
                        connectionAcctInfoWebService.IsPublicObject = this.IsAnonymous(dataSource, type);
                        resultControl = connectionAcctInfoWebService;
                        break;
                    case "SQLAcctInfo":
                        SqlAccountInformation connectionAcctInfoSQL = new SqlAccountInformation();
                        connectionAcctInfoSQL.IsTrusted = this.IsTrusted(dataSource, type);
                        resultControl = connectionAcctInfoSQL;
                        break;
                    case "CloudDBAcctInfo":
                        resultControl = new CloudDatabaseAccountInformation();
                        break;
                    case "AmazonWebServiceStoreInfo":
                        resultControl = new WebServiceStoreInformation();
                        break;
                    case "CloudDBStoreInfo":
                        resultControl = new CloudDatabaseStoreInformation();
                        break;
                    case "SQLStoreInfo":
                        resultControl = new SqlStoreInformation();
                        break;
                    case "WebServiceAcctInfo":
                        resultControl = new WebServiceConnection();
                        break;
                }
            }

            return resultControl;
        }

        /// <summary>
        /// Gets the trident connection object.
        /// </summary>
        /// <param name="dataSource">The data source.</param>
        /// <returns>string</returns>
        public string GetDataSourceType(string dataSource)
        {
            string query = "Connection/Source[@name='" + dataSource + "']";
            XmlNode xmlNode = this.xmlDocument.SelectSingleNode(query);
            return xmlNode.Attributes["type"].Value;
        }
    }
}
